//
//  JHMineViewController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/8.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHMineViewController.h"
#import "JHLoginViewController.h"
#import "JHMineWordViewController.h"
#import <YWFeedbackFMWK/YWFeedbackKit.h>
#import <YWFeedbackFMWK/YWFeedbackViewController.h>
#import "JHNavigationController.h"
#import "JHMyNewWordsListController.h"

//上线前需要修改自己的appID

static NSString * const kIDAppStore = @"1464906850";

static NSString * const kAppKey = @"23773927";
static NSString * const kAppSecret = @"00a82cb46c77ab76f3109022c13ecadf";

@interface JHMineViewController ()
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *headerConstraintHeight;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *navTitleConstraintHeight;
@property (weak, nonatomic) IBOutlet UILabel *versionLable;
@property (weak, nonatomic) IBOutlet UIImageView *isVersionImg;
@property (strong, nonatomic) IBOutlet UILabel *loginNameLabele;
@property (strong, nonatomic) IBOutlet UIView *loginView;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (nonatomic, strong) YWFeedbackKit *feedbackKit;
@end

@implementation JHMineViewController
//是否需要更新版本
{
    NSString *_appStoreVer;
    BOOL _update;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=@"个人中心";
    self.view.backgroundColor=JKGlobalBgColor;
}

#pragma mark  --隐藏导航栏

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    if ([MFDataTool getUserId].length>0) {
        self.loginNameLabele.text=[MFDataTool getUserName];
        [self.loginBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    }else{
        self.loginNameLabele.text=@"未登录";
        [self.loginBtn setTitle:@"立即登录" forState:UIControlStateNormal];
    }
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark getter
- (YWFeedbackKit *)feedbackKit {
    if (!_feedbackKit) {
        // SDK初始化，手动配置appKey/appSecret
       _feedbackKit = [[YWFeedbackKit alloc] initWithAppKey:kAppKey appSecret:kAppSecret];
        
        // 请从控制台下载AliyunEmasServices-Info.plist配置文件，并正确拖入工程
        //_feedbackKit = [[YWFeedbackKit alloc] autoInit];
    }
    return _feedbackKit;
}

- (void)initUI
{
    if (iPhone5) {
        self.headerConstraintHeight.constant=160;
        self.navTitleConstraintHeight.constant=85;
    }else if(iPhoneX || iPhoneXsMax || iPhoneXr){
        self.headerConstraintHeight.constant=200;
        self.navTitleConstraintHeight.constant=104;
    }else{
        self.navTitleConstraintHeight.constant=110;
    }
    self.headerView.backgroundColor=JKGlobalBgColor;
    
    self.versionLable.text=[NSString stringWithFormat:@"版本号%@",[MFAppTool getAppVersion]];
    
    _update = NO;
    self.isVersionImg.hidden=YES;
    NSString *URLString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",kIDAppStore] ;//id要改
    [DJHttpTool getWithURL:URLString params:nil success:^(id json) {
          self->_appStoreVer = [json[@"results"] firstObject][@"version"];
        if ([[MFAppTool getAppVersion] floatValue] <[self->_appStoreVer floatValue]) {
            self->_update=YES;
            self.isVersionImg.hidden=NO;
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (IBAction)btnDidClick:(UIButton *)btn {
    if (btn.tag==100) { //关于我们
        JHNEWebViewController *vc=[[JHNEWebViewController alloc]init];
        vc.urlStr=[NSString stringWithFormat:@"%@%@",BaseUrl,@"/junirthml/about_us.html"];
        vc.navTitle=@"关于我们";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (btn.tag==101){ //我的生词本
        [self.navigationController pushViewController:[JHMyNewWordsListController new] animated:YES];
    }else if (btn.tag==102){ //问题反馈
        [self faceBackControlller];
    }else if (btn.tag==103){//检查更新
        [self updateAppVeriosn];
    }else { //分享好友
        DXShareView *shareView = [[DXShareView alloc] init];
        DXShareModel *shareModel = [[DXShareModel alloc] init];
        [shareView showShareViewWithDXShareModel:shareModel shareContentType:DXShareContentTypeLink];
    }
}

- (IBAction)loginBtnDidClick:(UIButton *)sender {
    if ([sender.currentTitle isEqualToString:@"立即登录"]) {
        JHLoginViewController *vc=[[JHLoginViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [self showAlert:@"提示" message:@"确认退出账号吗?" buttonText:@"确定" buttonHandler:^(UIAlertAction *action) {
            self.loginNameLabele.text=@"未登录";
            [self.loginBtn setTitle:@"立即登录" forState:UIControlStateNormal];
            [kUserDefaults removeObjectForKey:@"userid"];
            [kUserDefaults removeObjectForKey:@"username"];
            [kUserDefaults synchronize];
        } cancelText:@"取消" cancelHandler:^(UIAlertAction *action) {
            
        }];
    }
}

#pragma mark---   有新版本跳转到appStore

- (void)updateAppVeriosn
{
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/revogihome/id%@?mt=8",kIDAppStore]]];
}

//跳转到意见反馈界面
- (void)faceBackControlller
{
    
    __weak typeof(self) weakSelf = self;
    [self.feedbackKit makeFeedbackViewControllerWithCompletionBlock:^(YWFeedbackViewController *viewController, NSError *error) {
        if (viewController != nil) {
            JHNavigationController *nav = [[JHNavigationController alloc] initWithRootViewController:viewController];
            [weakSelf presentViewController:nav animated:YES completion:nil];
            [viewController setCloseBlock:^(UIViewController *aParentController){
                [aParentController dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }];
    
    
    
}



@end
